﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
	<?php		
		echo '<p> Message JUMP!<p></br>';
		
		$response = $_GET['response'];
		$message_id = $_GET['message_id'];
		
		$dbc = mysqli_connect( 'localhost' ,'root' , '', 'teresa' ) or die( 'Error connecting to MySQL server.');		

		
		

 		if( $response == 'yes' ){
			$query = "SELECT response_id FROM teresa_message WHERE message_id = '$message_id'";
			$data = mysqli_query( $dbc , $query) or die('ERROR find message queerying database.');
			//Find response_id
			if( mysqli_num_rows( $data ) == 1 ){
				$row = mysqli_fetch_array($data);		
				$response_id = $row['response_id'];
				
				//select response_id and check the maximum count
				$query = "SELECT maximum,response FROM teresa_response WHERE response_id = '$response_id'";
				$data = mysqli_query( $dbc , $query) or die('ERROR find maximum, response queerying database.');
				echo $response_id.'<p> JUMP!<p></br>';

				if( mysqli_num_rows( $data ) == 1 ){
					
				
					$row = mysqli_fetch_array($data);	
					$response_maximum = $row['maximum'];
					$response_response = $row['response'];
				
					
					//update response
					if( $response_maximum > $response_response ){

						//message's response type is changed to user's response
						$query = "UPDATE teresa_message SET response = '$response' WHERE message_id = '$message_id'";	
						mysqli_query( $dbc , $query) or die('ERROR set response queerying database.');

						//response's count plus one
						$query = "UPDATE teresa_response SET response = $response_response + 1 WHERE response_id = $response_id";	
						mysqli_query( $dbc , $query) or die('ERROR set response count queerying database.');
						echo '<p> you select this message<p></br>';


						//check for full maximum count
						if( $response_maximum <= $response_response + 1 ){
							$query = "SELECT * FROM teresa_message WHERE response_id = '$response_id'";
							$data = mysqli_query( $dbc , $query) or die('ERROR response id message queerying database.');
							while( mysqli_num_rows( $data ) ){
								$row = mysqli_fetch_array($data);	

								$message_id_full = $row['message_id'];
								
								//update full response message count

								$query = "UPDATE teresa_message SET response = 'FULL' WHERE message_id = $message_id_full AND response = 'none'";							
								mysqli_query( $dbc , $query) or die('ERROR response_id full update queerying database.');						
							}
						}
					}else{
						echo '<p> this message already full<p></br>';					
					}
				}
			}

		}else if($response == 'no' ){
			echo '<p> you pass this message<p></br>';
		//	$home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/sender_reciever.php?message_id=' . $message_id . '&user_id=' . $user_id . '&response_id=' . $response_id . '&send_Count=' . $maximum . '&response=' . $response;
		//	header('Location:' . $home_url );         
	
		}
		echo '<a href="inbox.php">Return to inbox</a>';
	?>
</body>
</html>
